package org.springblade.modules.alimqtt;

import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.onsmqtt20200420.AsyncClient;
import com.aliyun.sdk.service.onsmqtt20200420.models.*;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import darabonba.core.client.ClientOverrideConfiguration;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.CompletableFuture;

@RestController
@AllArgsConstructor
@RequestMapping("/mqttDevice")
@Api(value = "软件升级-设备app版本表", tags = "设备app版本表接口")
@Slf4j
public class DeviceTestController {

	/**
	 * 详情
	 */
	@GetMapping("/initDeviceInfo")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "传入设备信息", notes = "传入设备信息")
	private void test() throws Exception{
		//LTAI5tFmA6kGR8MLApBWRUvD
		//0H8l6xziV3xGjY9sqEQKnMLWMwmofD
		StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
			.accessKeyId("LTAI5tFmA6kGR8MLApBWRUvD")
			.accessKeySecret("0H8l6xziV3xGjY9sqEQKnMLWMwmofD")
			.build());

		// Configure the Client
		AsyncClient client = AsyncClient.builder()
			.credentialsProvider(provider)
			.overrideConfiguration(
				ClientOverrideConfiguration.create()
					// Endpoint 请参考 https://api.aliyun.com/product/OnsMqtt

					.setEndpointOverride("onsmqtt.cn-hangzhou.aliyuncs.com")
				//.setConnectTimeout(Duration.ofSeconds(30))
			)
			.build();
		RegisterDeviceCredentialRequest build = RegisterDeviceCredentialRequest.builder()
			.clientId("GID_DOOR@@@700000")
			.instanceId("post-cn-5gu4bjgoz05").build();
		CompletableFuture<RegisterDeviceCredentialResponse> response = client.registerDeviceCredential(build);
		RegisterDeviceCredentialResponse registerDeviceCredentialResponse = response.get();
		RegisterDeviceCredentialResponseBody.DeviceCredential deviceCredential = registerDeviceCredentialResponse.getBody().getDeviceCredential();
		String clientId = deviceCredential.getClientId();
		String deviceAccessKeySecret = deviceCredential.getDeviceAccessKeySecret();
		String deviceAccessKeyId = deviceCredential.getDeviceAccessKeyId();
		System.out.println("clientId"+clientId);
		System.out.println("keySecret"+deviceAccessKeySecret);
		System.out.println("deviceAccessKeyId"+deviceAccessKeyId);

	}



}
